let TYPE_A4 = 1;
let TYPE_A5 = 2;
let TYPE_A4_T = 3;
let TYPE_S0 = 4;

let supportedNote = [

    {id: "0001", startPage: 51, endPage: 80, paperType: TYPE_A4},
    {id: "0018", startPage: 1, endPage: 50, paperType: TYPE_A4},
    {id: "0023", startPage: 1, endPage: 108, paperType: TYPE_A4_T},
    {id: "0024", startPage: 767, endPage: 966, paperType: TYPE_A4},
    {id: "0025", startPage: 967, endPage: 1166, paperType: TYPE_A4},
    {id: "0026", startPage: 1, endPage: 200, paperType: TYPE_S0},

];
let physicalPage =
    {
        1: {idx: 1, type: TYPE_A4},
        2: {idx: 2, type: TYPE_A4},
        3: {idx: 3, type: TYPE_A4},
        4: {idx: 4, type: TYPE_A4},
        5: {idx: 5, type: TYPE_A4},
        6: {idx: 6, type: TYPE_A4},
        7: {idx: 7, type: TYPE_A4},
        8: {idx: 8, type: TYPE_A4},
        9: {idx: 9, type: TYPE_A4},
        10: {idx: 10, type: TYPE_A4},
        11: {idx: 11, type: TYPE_A4},
        12: {idx: 12, type: TYPE_A4},
        13: {idx: 13, type: TYPE_A4},
        14: {idx: 14, type: TYPE_A4},

        15: {idx: 1, type: TYPE_A5},
        16: {idx: 2, type: TYPE_A5},
        17: {idx: 3, type: TYPE_A5},
        18: {idx: 4, type: TYPE_A5},

        19: {idx: 1, type: TYPE_A4_T},
        20: {idx: 2, type: TYPE_A4_T},

        21: {idx: 15, type: TYPE_A4},
        22: {idx: 16, type: TYPE_A4},
        23: {idx: 17, type: TYPE_A4},
        24: {idx: 18, type: TYPE_A4},
        25: {idx: 19, type: TYPE_A4},
        26: {idx: 20, type: TYPE_A4},
        27: {idx: 21, type: TYPE_A4},
        28: {idx: 22, type: TYPE_A4},
        29: {idx: 23, type: TYPE_A4},

        30: {idx: 1, type: TYPE_S0}
    };


class DeviceSpec {

    constructor(width, height, rowNum, columnNum) {

        this.width = width;
        this.height = height;
        this.rowNum = rowNum;
        this.columnNum = columnNum;
    }

    getCodeNumX() {
        return Math.ceil(mmtoPx(600, this.width) / 45);
    }

    getCodeNumY() {

        return Math.ceil(mmtoPx(600, this.height) / 45);
    }

}

let deviceSpec =
    {
        [TYPE_A4]: new DeviceSpec(210, 297, 9, 6),
        [TYPE_A4_T]: new DeviceSpec(297, 210, 6, 9),
        [TYPE_S0]: new DeviceSpec(30, 20, 60, 90),
    };

function mmtoPx(dpi, mm) {

    return Math.floor(mm / 25.4 * dpi);
}

function decodePage(pageCode, x, y) {

    var idx = physicalPage[pageCode].idx;
    var divideType = physicalPage[pageCode].type;
    var device = deviceSpec[divideType];

    let codeIdxX = Math.ceil(x / 32);
    let codeIdxY = Math.ceil(y / 32);
    let lPage;
    // console.log(idx, physicalPage, pageCode, deviceSpec, divideType);
    try {
        let xOffset = Math.ceil(codeIdxX / device.getCodeNumX());
        let yOffset = Math.ceil(codeIdxY / device.getCodeNumY());
        let realPageStart = (idx - 1) * (device.rowNum * device.columnNum);
        lPage = realPageStart + (yOffset - 1) * device.rowNum + xOffset;
    } catch (e) {
        console.log(e);
        return false
    }
    var note;
    for (let i = 0; i < supportedNote.length; i++) {
        let curNote = supportedNote[i];
        if (lPage >= curNote.startPage && lPage <= curNote.endPage && divideType == curNote.paperType) {
            note = curNote;
            break;
        }
    }
    if (!note) {

        return false;
    }
    // console.log({
    //     x: x % (32 * device.getCodeNumX()) / 32 * 1.905,
    //     y: y % (32 * device.getCodeNumY()) / 32 * 1.905,
    //     pageCode: lPage - note.startPage + 1,
    //     id: note.id
    // })
    return {
        x: x % (32 * device.getCodeNumX()) / 32 * 1.905,
        y: y % (32 * device.getCodeNumY()) / 32 * 1.905,
        pageCode: lPage - note.startPage + 1,
        id: note.id
    }

}

export {decodePage, supportedNote}
